﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Linq;
using System.Windows;

namespace ToolExcel
{
    public static partial class XTool
    {
        public static void 导出ClassServerGo()
        {
            导出ClassServerGo1();
            导出ClassServerGo2();
        }
        public static string 导出ClassServerGo类型转换(string vtype)
        {
            switch (vtype)
            {
                case "int":
                    return "int32";
                case "long":
                    return "int64";
                case "float":
                    return "float64";
                case "List<string>":
                    return "[]string";
                case "List<int>":
                    return "[]int32";
                case "List<long>":
                    return "[]int64";
                case "List<float>":
                    return "[]float64";
                case "List<List<string>>":
                    return "[][]string";
                case "List<List<int>>":
                    return "[][]int32";
                //case "Dictionary<string,string>":
                //    return "[]string";
                //case "Dictionary<int,int>":
                //case "Dictionary<long,long>":
                //case "Dictionary<string, string>":
                //case "Dictionary<int, int>":
                //case "Dictionary<long, long>":
                default:
                    return vtype;
            }
        }
        public static void 导出ClassServerGo1()
        {
            string path_class = PathClass + "\\" + "confstruct.go";
            string stread = "";
            if (!全部导出)
            {
                FileStream fsr_class = new FileStream(path_class, FileMode.Open);
                StreamReader sr_class = new StreamReader(fsr_class, Encoding.UTF8);
                stread = sr_class.ReadToEnd();
                sr_class.Close();
            }

            var sbw = new StringBuilder();

            sbw.AppendLine($"package conf");

            foreach (var nn in DictPages.Values)
            {
                if (!(nn.ValidType == EValidType.公共 || nn.ValidType == EValidType.仅服务器))
                {
                    continue;
                }
                var sbnode = new StringBuilder();
                sbnode.AppendLine($"type {nn.Name} struct {{");
                for (int iii = 0; iii < nn.Head.Count; iii++)
                {
                    if (nn.Head[iii] == "")
                    {
                        continue;
                    }
                    if (nn.TypeServer[iii] == "")
                    {
                        continue;
                    }
                    sbnode.Append($"\t{nn.Head[iii]} {导出ClassServerGo类型转换(nn.TypeServer[iii])} //{nn.HeadC[iii]}\r\n");
                }
                sbnode.AppendLine($"}}");
                string stnode = sbnode.ToString();
                string stnode1 = $"//Class{nn.Name}Start\r\n";
                string stnode2 = $"//Class{nn.Name}End\r\n";
                if (全部导出)
                {
                    sbw.Append(stnode1);
                    sbw.Append(stnode);
                    sbw.Append(stnode2);
                }
                else
                {
                    string stnodeold = stread.GetMidStr(stnode1, stnode2);
                    stread = stread.Replace(stnodeold, stnode);
                }
            }

            //Dictionary<string, Dictionary<string, string>> DictDictEnums = new Dictionary<string, Dictionary<string, string>>();
            //foreach (var kvp in DictDictEnum1) { DictDictEnums[kvp.Key] = kvp.Value; }
            //foreach (var kvp in DictDictEnum2) { DictDictEnums[kvp.Key] = kvp.Value; }
            //foreach (var kvp in DictDictEnums)
            //{
            //    var sbnode = new StringBuilder();
            //    sbnode.Append($"\tpublic enum E{kvp.Key}\r\n");
            //    sbnode.Append($"\t{{\r\n");
            //    long min = 0, max = 0;
            //    foreach (var kvp2 in kvp.Value)
            //    {
            //        sbnode.Append($"\t\t{kvp2.Key} = {kvp2.Value},\r\n");
            //        long.TryParse(kvp2.Value, out long tempmax);
            //        min = Math.Min(min, tempmax);
            //        max = Math.Max(max, tempmax);
            //    }
            //    sbnode.Append($"\t}}\r\n");
            //    string stnode = sbnode.ToString();
            //    string stnode1 = $"//Enum{kvp.Key}Start\r\n";
            //    string stnode2 = $"//Enum{kvp.Key}End\r\n";
            //    if (全部导出)
            //    {
            //        sbw.Append(stnode1);
            //        sbw.Append(stnode);
            //        sbw.Append(stnode2);
            //    }
            //    else
            //    {
            //        string stnodeold = stread.GetMidStr(stnode1, stnode2);
            //        stread = stread.Replace(stnodeold, stnode);
            //    }
            //}
            //sbw.Append($"}}\r\n");

            XGlobal.DeleteFile(path_class);
            FileStream fs_class = new FileStream(path_class, FileMode.OpenOrCreate);
            StreamWriter sw_class = new StreamWriter(fs_class, Encoding.UTF8);
            if (全部导出)
            {
                sw_class.Write(sbw);
            }
            else
            {
                sw_class.Write(stread);
            }
            sw_class.Close();
        }
        public static void 导出ClassServerGo2()
        {
            string path_class = PathClass + "\\" + "conf.go";
            string stread = "";
            if (!全部导出)
            {
                FileStream fsr_class = new FileStream(path_class, FileMode.Open);
                StreamReader sr_class = new StreamReader(fsr_class, Encoding.UTF8);
                stread = sr_class.ReadToEnd();
                sr_class.Close();
            }

            var sbw = new StringBuilder();

            sbw.AppendLine($"package conf\r\n");

            sbw.AppendLine($"import (");
            sbw.AppendLine($"	\"GoServer/srvbase/jsh\"");
            //sbw.AppendLine($"	\"fmt\"");
            sbw.AppendLine($")");
            sbw.AppendLine($"var confjsonpath = \"Config\\\\Base\\\\Json\\\\\"");


            var sbg1 = new StringBuilder();
            var sbg2 = new StringBuilder();

            foreach (var nn in DictPages.Values)
            {
                if (!(nn.ValidType == EValidType.公共 || nn.ValidType == EValidType.仅服务器))
                {
                    continue;
                }
                {
                    var sbnode = new StringBuilder();
                    var confname = nn.Name.Replace("Config", "Conf");
                    sbnode.AppendLine($"//{nn.NameCn}\t// {nn.NameFile}");
                    sbnode.AppendLine($"var {confname} map[string]{nn.Name}");
                    string stnode = sbnode.ToString();
                    string stnode1 = $"//Class{nn.Name}Start1\r\n";
                    string stnode2 = $"//Class{nn.Name}End1\r\n";
                    if (全部导出)
                    {
                        sbg1.Append(stnode1);
                        sbg1.Append(stnode);
                        sbg1.Append(stnode2);
                    }
                    else
                    {
                        string stnodeold = stread.GetMidStr(stnode1, stnode2);
                        stread = stread.Replace(stnodeold, stnode);
                    }
                }
                {
                    var sbnode = new StringBuilder();
                    sbnode.AppendLine($"//{nn.NameCn}\t// {nn.NameFile}");
                    var confname = nn.Name.Replace("Config", "Conf");
                    sbnode.AppendLine($"	{confname} = make(map[string]{nn.Name})");
                    sbnode.AppendLine($"	jsh.F2O(confjsonpath + \"{nn.Name}.json\", &{confname})");
                    sbnode.AppendLine($"	//fmt.Println(jsh.O2J({confname}))");
                    string stnode = sbnode.ToString();
                    string stnode1 = $"//Class{nn.Name}Start2\r\n";
                    string stnode2 = $"//Class{nn.Name}End2\r\n";
                    if (全部导出)
                    {
                        sbg2.Append(stnode1);
                        sbg2.Append(stnode);
                        sbg2.Append(stnode2);
                    }
                    else
                    {
                        string stnodeold = stread.GetMidStr(stnode1, stnode2);
                        stread = stread.Replace(stnodeold, stnode);
                    }
                }
            }

            sbw.Append(sbg1);
            sbw.AppendLine($"func ConfInit() {{");
            sbw.Append(sbg2);
            sbw.AppendLine($"}}");


            XGlobal.DeleteFile(path_class);
            FileStream fs_class = new FileStream(path_class, FileMode.OpenOrCreate);
            StreamWriter sw_class = new StreamWriter(fs_class, Encoding.UTF8);
            if (全部导出)
            {
                sw_class.Write(sbw);
            }
            else
            {
                sw_class.Write(stread);
            }
            sw_class.Close();
        }

    }
}
